package com.dr.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dr.pojo.Dept;
import com.dr.pojo.User;
import com.dr.service.DeptService;
import com.dr.service.UserService;

@Controller
@RequestMapping("/userManage")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	DeptService deptService;

	@RequestMapping("/userList")
	public ModelAndView userList(String txtUserName) {
		ModelAndView mAndView = new ModelAndView();
		List<User> uList = userService.findUserListByName(txtUserName);
		
		mAndView.addObject("ulist", uList);
		mAndView.setViewName("userManage/userList");
		return mAndView;
	}

	// ---------------------------------- 增加 ---------------------------------
	// ============= 增加用户页面 =============
	@RequestMapping("/userAdd")
	public ModelAndView userAdd() throws Exception {
		ModelAndView mav = new ModelAndView();
		List<Dept> deptList = deptService.findDeptList();
		mav.addObject("attrDeptList", deptList);
		mav.setViewName("userManage/userAdd");
		return mav;
	}

	// ============= 执行增加操作 =============
	@RequestMapping("/doUserAdd")
	public ModelAndView doUserAdd(HttpServletRequest request, User user) throws Exception {
		HttpSession session = request.getSession();
		User target = userService.findUserByAccount(user.getAccount());
		ModelAndView mav = new ModelAndView();
		if (target == null) {// 账户不存在
			User loginUser = (User) session.getAttribute("loginUser");
			Long id = loginUser.getId();
			user.setCreateUser(id + "");
			userService.insertUser(user);
			mav = userList("");
		} else {
			mav.addObject("attrInfoUserAdd", "系统帐户已经存在，请重新输入");
			mav.setViewName("userManage/userAdd");
		}
		return mav;
	}

	// ---------------------------------- 修改 ---------------------------------
	// ============= 修改用户页面 =============
	@RequestMapping("/userUpdate")
	public ModelAndView userUpdate(Integer id) throws Exception {
		User user = userService.findUserById(id);
		ModelAndView mav = new ModelAndView();
		List<Dept> deptList = deptService.findDeptList();
		mav.addObject("attrDeptList", deptList);
		mav.addObject("attrUser", user);
		mav.setViewName("/userManage/userUpdate");// WEBINF/jsp//[userManage/userUpdate].jsp
		return mav;
	}

	// ============= 执行修改操作 =============
	@RequestMapping("/doUserUpdate")
	public ModelAndView doUserUpdate(HttpSession session, User user) throws Exception {
		User loginUser = (User) session.getAttribute("loginUser");
		Long id = loginUser.getId();
		user.setUpdateUser(id + "");
		userService.updateUser(user);
		return userList("");
	}
	//====删除操作==
	@RequestMapping("/doUserDelete")
	public ModelAndView doUserDelete(HttpSession session,User user) {
		User loginUser = (User) session.getAttribute("loginUser");
		Long id = loginUser.getId();
		user.setUpdateUser(id + "");
		userService.deleteUser(user);
		return userList("");
	}

}
